
package com.yuke.cloud.service.wmc.mq;

/**
 * MQ消息监听，当有订单时，通知道仓库管理员打印电子面单，发货等处理(通过向tmc模块发送mq消息处理，
 * tmc模块收到消息后由websocket向web推送消息)
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.yuke.cloud.common.base.constant.MQConstants;
import com.yuke.cloud.common.core.mq.AbstractMessageListener;
import com.yuke.cloud.common.core.mq.RabbitMetaMessage;
import com.yuke.cloud.common.core.mq.RabbitSender;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.uac.dto.WarehouseAdminUserDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.wmc.entity.WmsWarehouseShop;
import com.yuke.cloud.service.wmc.service.WmsTransGoodsService;
import com.yuke.cloud.service.wmc.service.WmsWarehouseGroupService;
import com.yuke.cloud.service.wmc.service.WmsWarehouseShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@Import(RabbitSender.class)
public class WmcMessageListener extends AbstractMessageListener {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RabbitSender rabbitSender;
    @Autowired
    private WmsWarehouseShopService wmsWarehouseShopService;
    @Override
    public void receiveMessage(Message message, MessageConverter messageConverter) {
        Object bizObj = messageConverter.fromMessage(message);  //json字符串
        JSONObject jsonObject = JSON.parseObject(bizObj.toString());
        System.err.println(jsonObject);
        String msgType = jsonObject.get("MessageType").toString();

        if (msgType.equals("test")) {

        } else if (msgType.equals("findWarehouseAdmin")) {

            String warehouseIds = jsonObject.getString("warehouseIds");
            if (warehouseIds == null) {
                String names = jsonObject.getString("shopIds");
                String[] split = names.split(",");
                List<Long> list = new ArrayList();
                for (int i = 0; i < split.length; i++) {
                    list.add(Long.valueOf(split[i]));
                }
                Example example = new Example(WmsWarehouseShop.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("shopId", list);
                List<WmsWarehouseShop> wmsWarehouseShops = wmsWarehouseShopService.selectByExample(example);
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < wmsWarehouseShops.size(); i++) {
                    if (i == wmsWarehouseShops.size() - 1) {
                        sb.append(wmsWarehouseShops.get(i).getWarehouseId().toString());
                    } else {
                        sb.append(wmsWarehouseShops.get(i).getWarehouseId().toString()).append(",");
                    }
                }
                warehouseIds = sb.toString();
            }

            Map map = new HashMap();
            map.put("MessageType", "needPrint");
            map.put("WarehouseIds", warehouseIds);
            map.put("body", jsonObject.get("body").toString());//"通知仓库管理员打印面单");
            RabbitMetaMessage rabbitMetaMessage = new RabbitMetaMessage();
            rabbitMetaMessage.setExchange(MQConstants.BUSINESS_EXCHANGE);
            rabbitMetaMessage.setRoutingKey(MQConstants.BUSINESS_KEY_TMC);
            rabbitMetaMessage.setPayload(JSON.toJSONString(map));
            try {
                rabbitSender.send(rabbitMetaMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (msgType.equals("")) {
            //别的消息
        }
    }
}

